$(function () {
  let layer = layui.layer
  let form = layui.form
  //**************************************加载列表轮播图
  function list() {
    $.ajax({
      url: "/admin/swipers",
      success: function (res) {
        $("tbody").html('')
        $.each(res.data, function (index, item) {
          let a
          if (item.swiperstatus == 1) {
            a = `<span class="layui-badge layui-bg-green change s" is=2 _id=${item.id}>√</span>`
          } else {
            a = `<span class="layui-badge layui-bg-cyan change s" is=1 _id=${item.id}>×</span>`
          }
          let tr = `<tr>
        <td>${item.id}</td>
        <td>
          <img src="http://localhost:8888/uploads/${item.swiperimg}">
        </td>
        <td>${item.swiperlink}</td>
        <td class="c">
          <!-- 未使用 -->
          ${a}
        </td>
        <td>
          <button type="button" class="layui-btn layui-btn-xs layui-btn-danger delete" _id=${item.id}>
        删除
      </button>
        </td>
      </tr>`
          $("tbody").append(tr)
        })

      }
    })
  }
  list()




  // *************************************轮播图状态切换  使用还是未使用
  $("tbody").on("click", ".s", function () {
    let id = $(this).attr("_id")
    let status = $(this).attr("is")
    $.ajax({
      url: `/admin/swipers/${id}`,
      type: "PUT",
      data: {
        status: status * 1
      },
      success: function (res) {
        layer.msg(res.message)
        list()
      }
    })
  })



  // ***************************************删除操作
  $("tbody").on("click", ".delete", function () {
    layer.confirm('确定真的要删除吗?', index => {
      layer.close(index);
      let id = $(this).attr("_id")
      $.ajax({
        url: `/admin/swipers/${id}`,
        type: "DELETE",
        success: function (res) {
          layer.msg(res.message)
          list()
        }
      })
    })
  });







  // ***************************************上传文件
  $('body').on('click', '#uploadSwiper', function () {
    $('#myfile').click()
  })
  // 监听文件选中事件
  $('body').on('change', '#myfile', function (e) {
    let files = e.target.files
    let fd = new FormData()
    $.each(files, function (index, item) {
      fd.append('swipers', item)
    });
    $.ajax({
      url: "/admin/swipers",
      type: "POST",
      data: fd,
      processData: false,
      contentType: false,
      success: function (res) {
        layer.msg(res.message)
        list()
      }
    })
    // 监听文件选中事件
    $('body').on('change', '#myfile', function (e) {
      let files = e.target.files
      var fd = new FormData()
      $.each(files, function (index, item) {
        fd.append('swipers', item)
      })



    })
  })
})